
// 获取可拖动区域
const dragArea = document.getElementById('drag-area');
let isDragging = false;
let startX, startY;
// 监听鼠标按下事件
dragArea.addEventListener('mousedown'
    , (e) => {
        isDragging = true;
        startX = e.screenX;
        startY = e.screenY;
        window.pywebview.api.startDrag();
    });

// 监听鼠标移动事件
document.addEventListener('mousemove', (e) => {
    if
        (isDragging) {
        const deltaX = e.screenX - startX;
        const deltaY = e.screenY - startY;
        // 调用 pywebview 的 API 拖动窗口
        // console.log('deltaX:', deltaX, 'deltaY:', deltaY);
        window.pywebview.api.mvWindow(deltaX, deltaY);
        startX = e.screenX;
        startY = e.screenY;
    }
});

// 监听鼠标松开事件
document.addEventListener('mouseup'
    , () => {
        isDragging = false
            ;
    });

document.addEventListener('DOMContentLoaded', () => {
    const leftPanel = document.getElementById('leftPanel');
    const rightPanel = document.getElementById('rightPanel');
    const resizer = document.getElementById('resizer');
    const connectBtn = document.getElementById('connectBtn');
    const connectionStatus = document.getElementById('connectionStatus');
    const terminalInput = document.getElementById('terminalInput');
    const messageInput = document.getElementById('messageInput');
    const sendBtn = document.getElementById('sendBtn');
    const clearBtn = document.getElementById('clearBtn');
    const recordBtn = document.getElementById('recordBtn');
    const chatContainer = document.getElementById('chatContainer');

    let isRecording = false;
    let isDragging = false;

    // 拖动调整大小
    resizer.addEventListener('mousedown', (e) => {
        isDragging = true;
        e.preventDefault();
        const startX = e.clientX;
        const leftWidth = leftPanel.offsetWidth;
        const totalWidth = leftPanel.parentElement.offsetWidth;

        const handleMouseMove = (e) => {
            if (!isDragging) return;
            e.preventDefault();
            const delta = e.clientX - startX;
            const newLeftWidth = leftWidth + delta;
            const minWidth = 200;
            const maxWidth = totalWidth - 200;

            if (newLeftWidth > minWidth && newLeftWidth < maxWidth) {
                const percentage = (newLeftWidth / totalWidth) * 100;
                leftPanel.style.width = `${percentage}%`;
                rightPanel.style.width = `${100 - percentage}%`;
            }
        };

        const handleMouseUp = () => {
            isDragging = false;
            document.removeEventListener('mousemove', handleMouseMove);
            document.removeEventListener('mouseup', handleMouseUp);
        };

        document.addEventListener('mousemove', handleMouseMove);
        document.addEventListener('mouseup', handleMouseUp);
    });
    // 连接按钮点击
    connectBtn.addEventListener('click', () => {
        connectionStatus.classList.remove('bg-red-500');
        connectionStatus.classList.add('bg-green-500');
        connectBtn.disabled = true;
        const statusText = connectionStatus.nextElementSibling;
        statusText.textContent = '已连接';
    });

    // 终端输入
    // terminalInput.addEventListener('keypress', (e) => {
    //     if (e.key === 'Enter') {
    //         const command = terminalInput.value;
    //         if (command.trim()) {
    //             const newLine = document.createElement('div');
    //             newLine.className = 'mb-1';
    //             newLine.innerHTML = `
    //   <span class="text-green-500">root@server</span>
    //   <span class="text-gray-400">:</span>
    //   <span class="text-blue-400">~</span>
    //   <span class="text-gray-400">$ </span>
    //   <span>${command}</span>
    // `;
    //             terminalInput.parentElement.insertAdjacentElement('beforebegin', newLine);
    //             terminalInput.value = '';
    //         }
    //     }
    // });

    // 发送消息
//     sendBtn.addEventListener('click', () => {
//         const message = messageInput.value.trim();
//         if (message) {
//             const userMessage = document.createElement('div');
//             userMessage.className = 'mb-4 max-w-[80%] ml-auto';
//             userMessage.innerHTML = `
//     <div class="p-3 rounded-lg bg-blue-600">
//       <div class="text-sm">${message}</div>
//     </div>
//   `;
//             chatContainer.appendChild(userMessage);
//             messageInput.value = '';

//             // 模拟AI回复
//             setTimeout(() => {
//                 const aiMessage = document.createElement('div');
//                 aiMessage.className = 'mb-4 max-w-[80%] mr-auto';
//                 aiMessage.innerHTML = `
//       <div class="p-3 rounded-lg bg-gray-700">
//         <div class="text-sm">收到您的消息，我来帮您处理...</div>
//       </div>
//     `;
//                 chatContainer.appendChild(aiMessage);
//                 chatContainer.scrollTop = chatContainer.scrollHeight;
//             }, 500);
//         }
//     });

    // 清空对话
    // clearBtn.addEventListener('click', () => {
    //     chatContainer.innerHTML = '';
    // });
    // 语音按钮
    recordBtn.addEventListener('click', () => {
        isRecording = !isRecording;
        if (isRecording) {
            recordBtn.classList.add('bg-red-500', 'hover:bg-red-600');
            recordBtn.innerHTML = '<i class="fas fa-microphone icon mr-1"></i>停止录音';
        } else {
            recordBtn.classList.remove('bg-red-500', 'hover:bg-red-600');
            recordBtn.innerHTML = '<i class="fas fa-microphone icon mr-1"></i>语音';
        }
    });

    // Enter键发送消息
    messageInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault();
            sendBtn.click();
        }
    });
});
